package adim;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class App extends MIDlet implements CommandListener, Runnable
{
	private Displayable _defaultDisplay;

	private ConfirmDialog _cDialog;

	private Display _display;

	public Command _CMD_EXIT;
	
	public App()
	{
		_CMD_EXIT = new Command("Exit", Command.EXIT, 0);
		_defaultDisplay = null;
		_cDialog = new ConfirmDialog(this);
	}


	protected void _exit()
	{
		destroyApp(false);
		notifyDestroyed();
	}

	protected void startBackgroundThread()
	{
		Thread t = new Thread(this);
		t.start();
	}

	protected void startBackgroundThread(String title, String msg)
	{
		_cDialog.setTitle(title);
		_cDialog.setMessage(msg);
		setCurrentDisplay(_cDialog.getDisplay());
	}

	public void startApp()
	{
		_display = Display.getDisplay(this);
		_display.setCurrent(getDefaultDisplay());
	}

	public void pauseApp() {}
	public void destroyApp(boolean unconditional) {}

	public void backgroundThread()
	{
		_exit();
	}

	public void run() {
		backgroundThread();
	}

	public void processCommands(Command c, Displayable s) {}

	public void commandAction(Command c, Displayable s)
	{
		if(c == _CMD_EXIT) {
			_exit();
		} else if (c == _cDialog.CMD_YES) {
			startBackgroundThread();
			return;
		} else if (c == _cDialog.CMD_BACK) {
			setCurrentDisplay(_defaultDisplay);
			return;
		}

		processCommands(c, s);
	}

	public Display getDisplay()
	{
		return _display;
	}

	public void setDefaultDisplay(Displayable s)
	{
		_defaultDisplay = s;
	}

	public Displayable getDefaultDisplay()
	{
		return _defaultDisplay;
	}

	public void setCurrentDisplay(Displayable s)
	{
		_display.setCurrent(s);
	}

	public Displayable getCurrentDisplay()
	{
		return _display.getCurrent();
	}
}
